package br.com.webcrm.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.webcrm.dao.QuestPerguntaDAO;
import br.com.webcrm.model.QuestPergunta;

@Repository(value = "questPerguntaDAO")
public class QuestPerguntaDAOImpl extends BaseDAOImpl<QuestPergunta, Integer> implements QuestPerguntaDAO{
	
	@Autowired
	public QuestPerguntaDAOImpl(
			@Qualifier("sessionFactory") SessionFactory factory) {
		super.setSessionFactory(factory);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public List<QuestPergunta> consultarPorIdQuestionario(Integer idQuestionario) {
		String[] nomes = {"idQuestionario"};
		Object[] valores = {idQuestionario};
		return consultarPorNamedQueryEParametros("QuestPergunta.findByIdQuestionario", nomes, valores);
	}

	@Override
	public Integer consultarSeqUltimaPerguntaPorIdQuestionario(
			Integer idQuestionario) {
		Criteria crit = getSession().createCriteria(QuestPergunta.class);
		crit.add(Restrictions.eq("questionario.idQuestionario", idQuestionario));
		crit.setProjection(Projections.max("sequenciaPergunta"));
		return (Integer) crit.uniqueResult();
	}

}
